package com.cb.controller.bak.system;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.cb.core.controller.BaseFormController;
import com.cb.domain.model.system.Media;
import com.cb.domain.service.system.MediaManager;

/**
 * 
 * @Author author name
 * Create Time:
 * <p>Class Description Here [TODO]</p>
 */
public class MediaFormController extends BaseFormController {
	
	@Autowired
	private MediaManager mediaManager=null;
	
	public MediaFormController() {
		 setCommandName("media");
	     setCommandClass(Media.class);
	}
	
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		
		Media media=(Media)command;
		String msg=null;
		//validate the logic business data here [TODO]
		if(media.getMediaId()==null){
			msg=getText("media.added",media.getMediaId());
		}else{
			msg=getText("media.updated",media.getMediaId());
		}
		
		mediaManager.save(media);
		
		saveMessage(request,msg);
		
		return new ModelAndView(getSuccessView());
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		Media media=null;
		String mediaId=request.getParameter("mediaId");
		if(StringUtils.isNotEmpty(mediaId)){
			media=mediaManager.get(new Long(mediaId));
		}else{
			media=new Media();
		}
		return media;
	}
	
}
